package com.soup.lab.spring.retry.service.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class AnnotatedRetryableSampleServiceImpl implements SampleService {

    private static final Logger logger = LoggerFactory.getLogger(AnnotatedRetryableSampleServiceImpl.class);

    @Override
    @Retryable(include = RuntimeException.class)
    public void invoke() {
        Random random = new Random();
        int mod = random.nextInt(100) % 4;
        logger.info("----invoke----> {}", mod);
        switch (mod) {
            case 0: throw new ArrayIndexOutOfBoundsException();
            case 1: throw new IndexOutOfBoundsException();
            case 2: throw new NumberFormatException();
            case 3: throw new RuntimeException();
        }
    }

    @Recover
    public void recover() {
        System.out.println("recover");
    }

}
